<template>
	<view>
		<!-- #ifdef MP-WEIXIN -->
		<myheader :leftType="2"  :noLeft="false" showTitle="我的收藏"></myheader>
		<!-- #endif -->
		 <view class="top" :style="{ top: `${statusBarHeight}px` }">
			<view class="top-item" :key="index" v-for="(item, index) in arr" :class="activeIndex == index ? 'top-item_active':''" @tap="itemClick(index)">{{ item }}</view>
			<view class="top-line" :class="activeIndex == 0? 'left13' : 'left62'"></view>
		 </view>
		 <scroll-view scroll-y="true" class="food" v-if="activeIndex == 0" @scrolltolower="scrolltolower">
			<view class="food-item" v-for="(item, index) in serviceList" :key="index">
				<image class="food-item-left" :src="item.mainPic"></image>
				<view class="food-item-mid">
					<view class="food-item-mid-top">{{ item.name }}</view>
					<view class="food-item-mid-bottom">
						<view class="food-item-mid-bottom-item" v-for="(ele, idx) in item.tag" :key="idx">{{ ele }}</view>
					</view>
				</view>
				<view class="food-item-right">
					<view class="food-item-right-item" @tap="cancelService(item)">
						<image class="food-item-right-item-top" src="../../static/qingkong.svg"></image>
						<view class="food-item-right-item-bottom">取消收藏</view>
					</view>
				</view>
			</view>
			<view v-if="serviceList.length == 0" class="no-data">暂无数据</view>
			<view v-if="isLoaded == 2" class="no-data">没有更多数据了~</view>
		 </scroll-view>
		 <scroll-view class="food" v-if="activeIndex == 1">
			<view class="food-item" v-for="(item, index) in 10" :key="index">
				<image class="food-item-left" src="../../static/logo.svg"></image>
				<view class="food-item-mid">
					<view class="food-item-mid-top">{{ index % 2 == 0 ? '我是名称':'很长的名称很长的名称很长的名称很长的名称' }}</view>
					<view class="food-item-mid-bottom">
						<view class="food-item-mid-bottom-item" v-for="(item, idx) in typeArr" :key="idx">{{ item }}</view>
					</view>
				</view>
				<view class="food-item-right">
					<view class="food-item-right-item">
						<image class="food-item-right-item-top" src="../../static/logo.svg"></image>
						<view class="food-item-right-item-bottom">取消收藏</view>
					</view>
				</view>
			</view>
			<view class="no-data">暂无数据</view>
		</scroll-view>
	</view>
</template>

<script>
	import myheader from '../../components/header/index.vue';
	export default {
		components: {
			myheader
		},
		beforeMount() {
			// #ifdef MP-WEIXIN
			let custom = wx.getMenuButtonBoundingClientRect();
			wx.getSystemInfo({
				success: res => {
					this.statusBarHeight = (res.statusBarHeight + (custom.top - res.statusBarHeight)) +
						custom.height + 10
				}
			});
			// #endif
		},
		onShow() {
			this.getCollection()
		},
		data() {
			return {
				activeIndex: 0,
				arr: [ '订阅服务' ],
				// arr: [ '订阅服务', '商品' ],
				typeArr: ['类型1', '类类型2型2', '类型1类型2类型2', '类型2类型2'],
				statusBarHeight: '',
				params: {
					pageSize: 10,
					page: 1
				},
				serviceList: [],
				goodList: [],
				isLoaded:1
			}
		},
		methods: {
			scrolltolower() {
				if (this.isLoaded == 1) {
					
					this.params.page++
					this.getCollection('add')
				}
			},
			cancelService(item) {
				let that = this
				uni.showModal({
					content: '确定要取消收藏吗',
					confirmColor: "#E02828",
					success: function (res) {
						if (res.confirm) {
							uni.showLoading({
								title: '取消中',
								mask: true
							})
							that.trueCancel(item)
						} else if (res.cancel) {
							return false;
						}
					},
				});
				
			},
			trueCancel(item) {
				this.$H.delete("portal/favorites/remove", { id: item.serviceId }, {
						'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
					}).then(res => {
						uni.hideLoading();
						if (res.code == 0) {
							this.getCollection()
							uni.showToast({
								title: "取消成功",
								icon: "none"
							})
						} else {
							uni.showToast({
								title: res.msg,
								icon: "none"
							})
						}
					});
			},
			async getCollection(flag) {
				if (this.activeIndex == 0) {
					let res = await this.$H.post('portal/favorites/searchServices', this.params);
					if (res.code == 0) {
						if (!res.data.data || res.data.data.length < this.params.pageSize) {
							this.isLoaded = 2
						}
						if (flag) {
							this.serviceList = this.serviceList.concat(res.data.data?res.data.data: [])
						} else {
							this.serviceList = res.data.data?res.data.data: [];
						}
					}
				}
				if (this.activeIndex == 1) {}
			},
			itemClick(index) {
				if (this.activeIndex !== index) {
					this.activeIndex = index
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
.top {
	width: 100%;
	height: 94rpx;
	display: flex;
	position: sticky;
	z-index: 10;
	left: 0;
	top:0;
	background: #f4f6f7;
	&-item {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		color: #2E4964;
		&_active {
			color: #1ABC9C;
			font-weight: bold;
		}
	}
	&-line {
		width: 20rpx;
		height: 8rpx;
		background: #1ABC9C;
		border-radius: 40rpx;
		position: absolute;
		bottom: 4px;
		transition: all .3s;
	}
}
.left12 {
	left: calc(25% - 10rpx)
}
.left13 {
	left: calc(50% - 10rpx)
}
.left62 {
	left: calc(75% - 10rpx)
}
.des {
	display: flex;
	align-items: center;
	justify-content: center;
	width:100%;
	height: 80rpx;
	font-size: 24rpx;
	color: #8292A2;
	border-top: 1px solid #d9dcde;
}
.food {
	width: calc(100% - 40rpx);
	padding: 0 20rpx;
	margin-top: 10px;
	height: 100vh;
	&-item {
		background: #fff;
		display: flex;
		margin-bottom: 24rpx;
		padding: 20rpx;
		border-radius: 20rpx;
		&-left {
			width: 128rpx;
			height: 128rpx;
			border-radius: 10px;
		}
		&-mid {
			flex: 1;
			max-width: 50%;
			display: flex;
			flex-direction: column;
			margin-left: 10rpx;
			justify-content: space-evenly;
			
			&-top {
				font-size: 32rpx;
				font-weight: bold;
				color:#2E4964;
				max-width: 400rpx;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			}
			&-bottom {
				display: inline-flex;
				white-space: nowrap;
				overflow: scroll;
				&-item {
					padding: 0 6rpx;
					height: 52rpx;
					border-radius: 32rpx;
					border: 2px solid #EAECEF;
					margin-right: 10rpx;
					color: #8292A2;
					font-size: 24rpx;
					line-height: 52rpx;
					text-align: center;
				}
			}
		}
		&-right {
			display: flex;
			width: 200rpx;
			align-items: center;
			&-item {
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				margin-left: 25rpx;
				&-top {
					width: 48rpx;
					height: 48rpx;
				}
				&-bottom {
					font-size: 24rpx;
					color: #1ABC9C;
				}
			}			
		} 
	}
} 
.color_active {
	color: #1ABC9C;
}
.no-data {
	display: flex;
	align-items: center;
	font-size: 24rpx;
	justify-content: center;
	height: 96rpx;
	line-height: 96rpx;
	width:100%;
	color: #cbd1d8;
}
</style>
